<?php
class Product_Service_ProductStocket
{
	public function __construct(){
		
	}

	public static function getByProductId($id,$warehouse_id=1){		
		$obj = new Product_Model_ProductStocket();
		return $obj->getByProductId($id,$warehouse_id);		
	}

	public static function getByCondition($condition=array(),$type='*',$order=array('product_id'), $pageSize=0, $page=1)
	{ 
		$obj=new Product_Model_ProductStocket();
		$rows= $obj->getByCondition( $condition,$type,$order, $pageSize,$page);	
		return $rows;
	}
	
	public function getAdapter(){
		$model = new Product_Model_ProductStocket();
		return $model->getAdapter();
	}
	
	public function update($row,$id,$warehouse_id=1){
		$model = new Product_Model_ProductStocket();
		return $model->update($row,$id,$warehouse_id);
	}
	
	public function updateByCond(array $row,array $cond){
		$model = new Product_Model_ProductStocket();
		return $model->updateByCond($row,$cond);
	}
	
	public static function create($row)
	{ 
		$obj=new Product_Model_ProductStocket();
		return $obj->add($row);
	}
	
	/*更新产品库存
	*param product_id=>产品ID qty=>数量
	*return true,false
	*/
	public static function updateQuantity($productid,$qty,$warehouse_id=1)
	{ 
		if($productid=='' || abs($qty)<1 ){
			return false;
		}
		$date=date("Y-m-d:H:i:s");
		$session = new Zend_Session_Namespace('userAuth');
		$user_id=$session->userID;					//操作人
		$obj=new Product_Model_ProductStocket();
		$logModel= new Product_Model_ProductStocketLog();
		$row=$obj->getByProductId($productid,$warehouse_id);
		$addRow['product_id']=$productid;
		$addRow['ps_quantity']=$qty;
		$addRow['ps_last_update']=$date;
		$tmp=0;
		$current_value=0;
		$result2=false;
		if($row){
			$tmp=$row['ps_quantity'];
			$current_value=$qty+$row['ps_quantity'];
			$result=$obj->update(array('ps_quantity'=>$current_value),$productid,$warehouse_id);
		}else{
			$current_value=$qty;
			$result=$obj->add($addRow);
		}
		if($result){
			//记录日志
			$addLog=array(
				'product_id'=>$productid,
				'user_id'=>$user_id,
				'psl_type'=>1, //库存修改
				'psl_current_value'=>$current_value,
				'psl_note'=>"Inventory change from ".$tmp." to ".$current_value,
			);
			$result2=$logModel->add($addLog);
		}
		if($result && $result2){
			return true;
		}else{
			return false;
		}
	}
	
	/*更新产品库存
	*param product_id=>产品ID qty=>数量
	*return true,false
	*/
	public static function updateQuantityHold($productid,$qty,$warehouse_id=1,$msg="")
	{ 
		if($productid=='' || $qty<0){
			return false;
		}
		$date=date("Y-m-d:H:i:s");
		$session = new Zend_Session_Namespace('userAuth');
		$user_id=$session->userID;					//操作人
		$obj=new Product_Model_ProductStocket();
		$logModel= new Product_Model_ProductStocketLog();
		$row=$obj->getByProductId($productid,$warehouse_id);
		
		$tmp=0;
		$current_value=0;
		$result2=false;
		
		$tmp=$row['ps_quantity_hold'];
		$current_value=$row['ps_quantity_hold']-$qty;
		$result=$obj->update(array('ps_quantity_hold'=>$current_value),$productid,$warehouse_id);
		
		if($result){
			//记录日志
			$addLog=array(
				'product_id'=>$productid,
				'user_id'=>$user_id,
				'psl_type'=>1, //库存修改
				"psl_current_value_hold"=>$row['ps_quantity_hold'],
				'psl_note'=>$msg." Blocked stock change from ".$tmp." to ".$current_value,
			);
			$result2=$logModel->add($addLog);
		}
		if($result && $result2){
			return true;
		}else{
			return false;
		}
	}
	
	
	/*
	*
	*产品下架
	*
	*/
	public static function pickingProcdut($productId,$quantity,$warehouse_id=1,$remark=""){
		$obj = new Product_Model_ProductStocket();
		$rowProduct = $obj->getByProductId($productId,$warehouse_id);

		if($quantity <= $rowProduct['ps_quantity']){
			$currentStock = $rowProduct['ps_quantity']-$quantity;
			$currentHold = $rowProduct['ps_quantity_hold']+$quantity;
			$timeNow = date('Y-m-d H:i:s');
			$whProduct =	array('ps_quantity'=>$currentStock ,
								'ps_quantity_hold'=>$currentHold,
								'ps_last_update'=>$timeNow);
							
			$result= $obj->update($whProduct,$productId,$warehouse_id);
			
			$logModel= new Product_Model_ProductStocketLog();			
			$result2=false;			
			
			if($result){
				//记录日志
				$addLog=array(
					'product_id'=>$productId,
					'user_id'=>"0",
					'psl_type'=>1, //库存修改
					"psl_current_value"=>$currentStock,
					"psl_current_value_hold"=>$currentHold,
					'psl_note'=>"picking Procdut ".$remark,
				);
				$result2=$logModel->add($addLog);
			}
			if($result && $result2){
				return true;				
			}
		}
		return false;
	}
	
}